package at.codebase.db.dao;

import static at.codebase.db.OfyService.ofy;

import java.util.ArrayList;
import java.util.List;

import at.codebase.db.entities.EItem;
import at.codebase.db.entities.EItemFeedback;
import at.codebase.db.entities.EUser;

/**
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 *
 */
public class EItemFeedbackDao {

	/**
	 * Adds a new ItemFeedback to the Database
	 * 
	 * ToDo List here:
	 * * Add to Database
	 * * Update Item data (MAYBE!)
	 * 
	 * @param new ItemFeedback
	 */
	public static void addItemFeedback(final EItemFeedback itemFeedbackEntity) {
		ofy().save().entity(itemFeedbackEntity).now();
	}
	
	/**
	 * Remove an ItemFeedback from the Database
	 * 
	 * ToDo List here:
	 * * Remove from Database
	 * * Update Item data (MAYBE!)
	 * 
	 * @param ItemFeedback
	 */
	public static void removeItemFeedback(final EItemFeedback itemFeedbackEntity) {
		ofy().delete().type(EItemFeedback.class).id(itemFeedbackEntity.getId().longValue());
	}
	
	public static EItemFeedback getFeedbackById(Long feedbackId) {
		if(feedbackId.longValue() <= 0)
			return null;
		return ofy().load().type(EItemFeedback.class).filter("itemFeedbackId", feedbackId).first()
				.get();
	}
	
	public static List<EItemFeedback> getFeedbackByItem(Long itemId){
		return ofy().load().type(EItemFeedback.class).filter("item", EItem.key(itemId)).list();
	}

	public static ArrayList<EItemFeedback> getFeedbackByAuthor(final Long userId) {
		return new ArrayList<EItemFeedback>(ofy().load().type(EItemFeedback.class).filter("author", EUser.key(userId)).list());
	}

	public static void removeItemFeedbacksById(ArrayList<Long> feedbackIds) {
		ofy().delete().type(EItemFeedback.class).ids(feedbackIds);
	}

}
